package com.daqsoft.data;

import com.fasterxml.jackson.annotation.JsonIdentityInfo;
import com.fasterxml.jackson.annotation.ObjectIdGenerators;
import org.hibernate.annotations.Type;
import org.springframework.context.annotation.Lazy;

import javax.persistence.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 模块数据对象
 * @author 周麟东 on 16:47
 */
@Entity
@Table(name = "d_module")
@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "children")
public class Module {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private int id;
    /**
     * 是否为叶子节点
     */
    @Type(type = "yes_no")
    private boolean leaf;
    /**
     * 模块名称
     */
    private String moduleName;
    /**
     * 模块地址
     */
    private String url;
    /**
     * 父级id
     */
    @ManyToOne(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
    private Module pModule;
    /**
     * 是否为项目节点
     */
    @Type(type = "yes_no")
    private boolean projectModule;
    /**
     * 子级叶子节点
     */
    @OneToMany(targetEntity = Module.class , mappedBy = "pModule" , fetch = FetchType.EAGER)
    private List<Module> children;
    /**
     * 图标地址
     */
    private String icon;
    /**
     * 顺序
     */
    private int level;
    public Map<String,Object> toMap() {

        Map<String,Object> map = new HashMap<>();
        map.put("id",this.id);
        map.put("leaf",this.leaf);
        map.put("moduleName",this.moduleName);
        map.put("url",this.url);
        if (this.pModule!=null){
            map.put("pid",this.pModule.id);
        }
        map.put("projectModule",this.projectModule);
        map.put("icon" , this.icon);
        map.put("level",this.level);
        return map;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public boolean isLeaf() {
        return leaf;
    }

    public void setLeaf(boolean leaf) {
        this.leaf = leaf;
    }

    public String getModuleName() {
        return moduleName;
    }

    public void setModuleName(String moduleName) {
        this.moduleName = moduleName;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public boolean isProjectModule() {
        return projectModule;
    }

    public void setProjectModule(boolean projectModule) {
        this.projectModule = projectModule;
    }

    public List<Module> getChildren() {
        return children;
    }

    public void setChildren(List<Module> children) {
        this.children = children;
    }

    public String getIcon() {
        return icon;
    }

    public void setIcon(String icon) {
        this.icon = icon;
    }

    public Module getpModule() {
        return pModule;
    }

    public void setpModule(Module pModule) {
        this.pModule = pModule;
    }

    public int getLevel() {
        return level;
    }

    public void setLevel(int level) {
        this.level = level;
    }
}
